package view;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import java.util.ArrayList;

import model.TockConstants;
import net.TockClient;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class TockAPP extends JFrame implements TockConstants {
	//----- What the hell is this for?
	public static final long serialVersionUID = 0;
	private JList gameList;
	private JButton createButton;
	private JButton joinButton;
	private JButton quitButton;
	private JLabel jLabel2;
	private JSpinner numPlayersInput;
	private JTextField gameNameInput;
	private JLabel jLabel1;
	private JButton updateButton;
	
	private TockClient 	theClient;
	private GameGui		theGUI;

	public TockAPP(TockClient theClient) {
		
		this.theClient = theClient;
		theGUI = null;
		
		getContentPane().setLayout(null);
		{
			gameList = new JList();
			getContentPane().add(gameList);
			gameList.setBounds(14, 21, 168, 189);
		}
		{
			createButton = new JButton();
			getContentPane().add(createButton);
			createButton.setText("New Game");
			createButton.setBounds(203, 56, 126, 70);
		}
		{
			joinButton = new JButton();
			getContentPane().add(joinButton);
			joinButton.setText("Join Game");
			joinButton.setBounds(343, 56, 119, 70);
		}
		{
			updateButton = new JButton();
			getContentPane().add(updateButton);
			updateButton.setText("Update Games");
			updateButton.setBounds(203, 140, 126, 70);
		}
		{
			quitButton = new JButton();
			getContentPane().add(quitButton);
			quitButton.setText("Quit");
			quitButton.setBounds(343, 140, 119, 70);
		}
		{
			jLabel1 = new JLabel();
			getContentPane().add(jLabel1);
			jLabel1.setText("Game Name:");
			jLabel1.setBounds(203, 0, 119, 28);
		}
		{
			jLabel2 = new JLabel();
			getContentPane().add(jLabel2);
			jLabel2.setText("Number of Players");
			jLabel2.setBounds(336, 0, 133, 28);
		}
		{
			gameNameInput = new JTextField();
			getContentPane().add(getGameNameInput());
			gameNameInput.setBounds(203, 28, 126, 21);
		}
		{
			SpinnerListModel numPlayersInputModel = new SpinnerListModel(
				new String[] { "0", "1", "2", "3", "4" });
			numPlayersInput = new JSpinner();
			getContentPane().add(getNumPlayersInput());
			numPlayersInput.setModel(numPlayersInputModel);
			numPlayersInput.setBounds(364, 28, 63, 21);
		}
		{
			setTitle("Tock " + VERSION);
			setVisible(true);
			this.setSize(489, 260);
			//this.setResizable(false);
		}
		addListeners();
		handleUpdate();
	}
	
	public void addListeners() {
		createButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				// Try to create the game
				int response = theClient.sendCreateGame(gameNameInput.getText(), Integer.parseInt(((String)numPlayersInput.getValue())));
				
				// Check the responce
				switch (response) {
				case(CREATE_GAME_ERROR_NONE):
					JOptionPane.showMessageDialog(getThis(), "Game created.");
					break;
				case(CREATE_GAME_ERROR_NAME):
					// error name
					JOptionPane.showMessageDialog(getThis(), "Duplicate game name.");
					break;
				case(CREATE_GAME_ERROR_PLAYERS):
					// error players
					JOptionPane.showMessageDialog(getThis(), "Bad number of players");
					break;
				case(CREATE_GAME_ERROR_FULL):
					// error full
					JOptionPane.showMessageDialog(getThis(), "Server is full of games. Sorry.");
					break;
				}
				
				// Update the list
				handleUpdate();
			}
		});
		
		joinButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				// Get the game name
				String gameToSplit = (String)gameList.getSelectedValue();
				if (null != gameToSplit) {
					// Get the game number
					int gameNum = theClient.sendJoinGame(gameToSplit.split(":")[0]);
					
					// Check if the game was valid
					if (-1 == gameNum) {
						JOptionPane.showMessageDialog(getThis(), "Invalid game name - " + (String)gameList.getSelectedValue());
					} else {
						// Create the new game.
						theGUI = new GameGui(theClient, getThis(), gameNum);
						
						// Hide ourselves
						hideThis();
					}
				} else {
					JOptionPane.showMessageDialog(getThis(), "Please select a game to join.");
				}
			}
		});
		
		updateButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				handleUpdate();
			}
		});
		
		quitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				System.out.println("Shutting down now...");
				if (null != theGUI) theGUI.goOffline();
				System.exit(0);
			}
		});
		
		gameList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent evt) {
				String gameToSplit = (String)gameList.getSelectedValue();
				if (null != gameToSplit) {
					String[] gameSplit = gameToSplit.split(":");
					gameNameInput.setText(gameSplit[0]);
					numPlayersInput.setValue(gameSplit[1]);
				}
			}
		});
	}
	
	public void handleUpdate() {
		ArrayList theGames = theClient.sendGameListRequest();
		gameList.setListData(theGames.toArray());
	}
	
	private TockAPP getThis() { return this; }
	private void hideThis() { this.setVisible(false); }

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// Should run the application to find what game to join.
		String host = JOptionPane.showInputDialog("Please enter host computer");
		TockAPP app = new TockAPP(new TockClient(host));
		
		app.validate();
	}
	
	public JTextField getGameNameInput() {
		return gameNameInput;
	}
	
	public JSpinner getNumPlayersInput() {
		return numPlayersInput;
	}

}
